package com.coda.config;

import com.coda.interceptor.JwtTokenInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
@Slf4j
public class WebMvcConfiguration extends  WebMvcConfigurationSupport{


    @Autowired
    private JwtTokenInterceptor jwtTokenInterceptor;

    /**
     * 自定义资源映射
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("swagger-ui.html", "doc.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }

    /**
     * swagger接口文档
     */

    public Docket docket(){
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("仓库管理系统接口文档")
                .version("2.0")
                .description("仓库管理系统接口文档")
                .build();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .groupName("接口管理")
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.coda.controller"))
                .paths(PathSelectors.any())
                .build();

        return docket;
    }

    /**
     * 注册自定义拦截器
     */
    protected void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义员工拦截器...");
        registry.addInterceptor(jwtTokenInterceptor)
                .addPathPatterns("/warehouse/**")
                .excludePathPatterns("/warehouse/login")
                .excludePathPatterns("/warehouse/doc.html")
                .excludePathPatterns("/warehouse/captcha/captchaImage");

    }
}
